package xyz.xtt.exchange.service.impl;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tongtong.share.client.ShareClient;
import com.tongtong.share.params.AuditRequest;
import com.tongtong.share.params.SendVerifyParams;
import com.tongtong.share.params.VideoAuditingParams;
import com.tongtong.share.response.AuditResponse;
import com.tongtong.share.response.BatchImageAuditingResponse;
import com.tongtong.share.response.ImageAuditResponse;
import com.tongtong.share.response.VideoAuditResponse;
import com.tongtong.share.vo.CredentialRes;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.api.enums.AssetTypeEnum;
import xyz.xtt.asset.client.AssetClient;
import xyz.xtt.asset.client.request.CardInfoRequest;
import xyz.xtt.asset.client.response.AssetAccountResponse;
import xyz.xtt.asset.client.response.AssetAllInfoResponse;
import xyz.xtt.asset.client.response.AssetDetailInfo;
import xyz.xtt.asset.client.response.AssetGroupResponse;
import xyz.xtt.asset.client.response.AssetVo;
import xyz.xtt.asset.client.response.BlindBoxDetailResponse;
import xyz.xtt.asset.client.response.NftListVo;
import xyz.xtt.asset.client.response.TblAssetCardDto;
import xyz.xtt.asset.client.response.TransactionResponse;
import xyz.xtt.common.response.ApiResult;
import xyz.xtt.equity.api.dto.TblUserBenefitByAssetIdDto;
import xyz.xtt.equity.client.EquityClient;
import xyz.xtt.equity.client.response.AssetEntitlementResponse;
import xyz.xtt.exchange.service.IInnerService;
import xyz.xtt.pay.api.enums.PayOrderBusinessTypeEnum;
import xyz.xtt.pay.client.PayClient;
import xyz.xtt.pay.client.request.BusinessOrderRequest;
import xyz.xtt.pay.client.response.BusinessOrderDto;
import xyz.xtt.pay.client.response.BusinessOrderResponse;
import xyz.xtt.pay.client.response.PayOrderResponse;
import xyz.xtt.pay.client.response.TblPayOrderDto;
import xyz.xtt.user.client.UserClient;
import xyz.xtt.user.client.response.UserInfoResponse;

@Slf4j
@Service
public class InnerServiceImpl implements IInnerService {
	@Autowired
	private AssetClient assetClient;
	@Autowired
	private UserClient userClient;
	@Autowired
	private EquityClient equityClient;
	@Autowired
	private PayClient payClient;
	@Autowired
	private ShareClient shareClient;

	@Override
	public UserInfoResponse userInfo(Long appUid) {
		ApiResult<UserInfoResponse> response = userClient.info(appUid);
		log.info("userClient userInfo one response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public List<UserInfoResponse> userInfo(Collection<Long> appUid) {
		ApiResult<List<UserInfoResponse>> response = userClient.batchInfo(appUid);
		log.info("userClient userInfo batch response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	@Override
	public Boolean checkAuth(String uid, Long authCheckFrequency) {
		ApiResult<Boolean> response = userClient.checkAuth(uid, authCheckFrequency);
		log.info("userClient checkAuth batch response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public List<NftListVo> index(Long uid, Integer type, Integer currPage, Integer pageSize, String sortType, String sortOrder) {
		ApiResult<List<NftListVo>> response = assetClient.index(uid, type, currPage, pageSize, sortType, sortOrder);
		log.info("userClient index response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetAllInfoResponse infoGoodsId(String goodsId) {
		ApiResult<AssetAllInfoResponse> response = assetClient.infoGoodsId(goodsId);
		log.info("assetClient infoGoodsId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetDetailInfo detailInfo(String uid, String goodsId, Long accountId) {
		ApiResult<AssetDetailInfo> response = assetClient.detailInfo(uid, goodsId, accountId);
		log.info("assetClient detailInfo response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public TransactionResponse queryNotPaiedPersonalMintUpChaninOrder(Long assetId, String appUid) {
		ApiResult<TransactionResponse> response = assetClient.queryNotPaiedPersonalMintUpChaninOrder(assetId, appUid);
		log.info("assetClient queryNotPaiedPersonalMintUpChaninOrder response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public List<AssetEntitlementResponse> assetEntitlementWithAssetId(Long assetId) {
		ApiResult<List<AssetEntitlementResponse>> response = equityClient.assetEntitlementWithAssetId(assetId);
		log.info("equityClient assetEntitlementWithAssetId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	@Override
	public AssetAccountResponse accountInfoCheckAmount(Long assetId, Long appUid) {
		ApiResult<AssetAccountResponse> response = assetClient.accountInfoCheckAmount(assetId, appUid);
		log.info("assetClient accountInfoCheckAmount response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AssetAccountResponse accountInfoUnCheckAmount(Long assetId, Long appUid) {
		ApiResult<AssetAccountResponse> response = assetClient.accountInfoUnCheckAmount(assetId, appUid);
		log.info("assetClient accountInfoUnCheckAmount response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public BigDecimal buyMaxPriceWithHistory(String appUid, String goodsId) {
		ApiResult<BigDecimal> response = assetClient.buyMaxPriceWithHistory(appUid, goodsId);
		log.info("assetClient buyMaxPriceWithHistory response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, BigDecimal.ZERO);
	}

	@Override
	public List<TblAssetCardDto> getUserCardInfo(CardInfoRequest cardInfoRequest) {
		ApiResult<List<TblAssetCardDto>> response = assetClient.getUserCardInfo(cardInfoRequest);
		log.info("assetClient getUserCardInfo response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	@Override
	public List<BusinessOrderDto> selectListByGoodsIds(BusinessOrderRequest businessOrderRequest) {
		ApiResult<List<BusinessOrderDto>> response = payClient.selectListByGoodsIds(businessOrderRequest);
		log.info("assetClient selectListByGoodsIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	@Override
	public AssetGroupResponse group(Long assetId) {
		ApiResult<AssetGroupResponse> response = assetClient.group(assetId);
		log.info("assetClient selectListByGoodsIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public TblPayOrderDto fetchPayOrder(Long payOrderId) {
		ApiResult<TblPayOrderDto> response = payClient.fetchPayOrder(payOrderId);
		log.info("assetClient selectListByGoodsIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public List<Long> entitlementDeliver(Long assetId, Long appUid) {
		ApiResult<List<Long>> response = equityClient.entitlementDeliver(assetId, appUid);
		log.info("equityClient entitlementDeliver response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	/**
	 * 查询用户权益数据
	 * 
	 * @param appUserId
	 * @param benefitCodes
	 * @return
	 */
	@Override
	public Map<String, Integer> getUserBenefits(long appUserId, String benefitCodes) {
		ApiResult<Map<String, Integer>> userBenefits = equityClient.getUserBenefits(appUserId, benefitCodes);
		log.info("equityClient getUserBenefits response:{}", JSONUtil.toJsonStr(userBenefits));
		return ApiResult.convert(userBenefits, Maps.newHashMap());
	}

	/**
	 * 用户权益消耗
	 * 
	 * @param appUserId
	 * @param benefitCode
	 * @param consumeNum
	 * @param operateBusinessId
	 * @param operateDesc
	 * @return
	 */
	@Override
	public Boolean userBenefitConsumed(long appUserId, String benefitCode, int consumeNum, Long operateBusinessId, String operateDesc) {
		ApiResult<Boolean> response = equityClient.userBenefitConsumed(appUserId, benefitCode, consumeNum, operateBusinessId, operateDesc);
		log.info("equityClient userBenefitConsumed response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	/**
	 * 用户权益消耗
	 *
	 * @param appUserId         用户id
	 * @param benefitCode       权益code
	 * @param consumeNum        消耗数量
	 * @param operateBusinessId 操作业务id。可空。比如铸造消耗权益时传铸造审核记录id
	 * @param operateDesc       操作描述。比如铸造消耗权益时传：铸造审核或更具体信息
	 * @return 返回实际消耗次数
	 */
	@Override
	public Integer userBenefitConsumeAsMuchAsPossible(long appUserId, String benefitCode, int consumeNum, Long operateBusinessId, String operateDesc) {
		ApiResult<Integer> response = equityClient.userBenefitConsumeAsMuchAsPossible(appUserId, benefitCode, consumeNum, operateBusinessId, operateDesc);
		log.info("equityClient userBenefitConsumeAsMuchAsPossible response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, 0);
	}

	@Override
	public Boolean userBenefitRecover(long appUserId, Long operateBusinessId, String operateDesc) {
		ApiResult<Boolean> response = equityClient.userBenefitRecover(appUserId, operateBusinessId, operateDesc);
		log.info("equityClient userBenefitConsumeAsMuchAsPossible response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public TblUserBenefitByAssetIdDto getBenefitDetailInfo(String appUserId, Long assetId) {
		ApiResult<TblUserBenefitByAssetIdDto> response = equityClient.getBenefitDetailInfo(appUserId, String.valueOf(assetId));
		log.info("equityClient getBenefitDetailInfo response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public List<AssetVo> queryAssetType(Integer currPage, String uid, AssetTypeEnum type) {
		ApiResult<List<AssetVo>> response = assetClient.queryAssetType(currPage, uid, type);
		log.info("assetClient queryAssetType response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	@Override
	public List<BlindBoxDetailResponse> queryBlindBoxDetail(List<String> goodsId) {
		ApiResult<List<BlindBoxDetailResponse>> response = assetClient.queryBlindBoxDetail(goodsId);
		log.info("assetClient queryBlindBoxDetail response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	@Override
	public PayOrderResponse queryPayStatus(Long businessRefId, Integer businessType) {
		ApiResult<PayOrderResponse> response = payClient.queryPayStatus(businessRefId, businessType);
		log.info("assetClient queryPayStatus response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public Map<String, AssetAllInfoResponse> batchInfoGoodsIds(Collection<String> goodsId) {
		ApiResult<Map<String, AssetAllInfoResponse>> response = assetClient.batchInfoGoodsIds(goodsId);
		log.info("assetClient batchInfoGoodsIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Maps.newConcurrentMap());
	}

	@Override
	public String receiveVerifyRequest(String certifyId) {
		ApiResult<String> response = shareClient.receiveVerifyRequest(certifyId);
		log.info("assetClient batchInfoGoodsIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, StringUtils.EMPTY);
	}

	@Override
	public BatchImageAuditingResponse batchImageAuditing(String imageUrl) {
		ApiResult<BatchImageAuditingResponse> response = shareClient.batchImageAuditing(imageUrl);
		log.info("assetClient batchInfoGoodsIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public Map<String, String> sendVerify(SendVerifyParams params) {
		ApiResult<Map<String, String>> response = shareClient.sendVerifyRequest(params);
		log.info("assetClient batchInfoGoodsIds response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Maps.newHashMap());
	}

	@Override
	public CredentialRes getCredential() {
		ApiResult<CredentialRes> response = shareClient.getCredential();
		log.info("shareClient getCredential response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public String imageToImage(String key, String style) {
		ApiResult<String> response = shareClient.imageToImage(key, style);
		log.info("shareClient getCredential response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, StringUtils.EMPTY);
	}

	@Override
	public VideoAuditResponse videoAuditing(VideoAuditingParams request) {
		ApiResult<VideoAuditResponse> response = shareClient.videoAuditing(request);
		log.info("shareClient videoAuditing response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public VideoAuditResponse receiveVideoAuditing(String jobId) {
		ApiResult<VideoAuditResponse> response = shareClient.receiveVideoAuditing(jobId);
		log.info("shareClient receiveVideoAuditing response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public List<ImageAuditResponse> describeAuditingImageRequestId(String jobId) {
		ApiResult<List<ImageAuditResponse>> response = shareClient.describeAuditingImageRequestId(jobId);
		log.info("shareClient receiveVideoAuditing response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Lists.newArrayList());
	}

	@Override
	public PayOrderResponse infoPayOrderWithBusinessRefId(String businessRefId, PayOrderBusinessTypeEnum businessType) {
		ApiResult<PayOrderResponse> response = payClient.infoPayOrderWithBusinessRefId(businessRefId, businessType);
		log.info("payClient infoPayOrderWithBusinessRefId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AuditResponse txAuditing(String key) {
		ApiResult<AuditResponse> response = shareClient.txAuditing(key);
		log.info("shareClient txAuditing response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public AuditResponse textAudit(AuditRequest auditRequest) {
		ApiResult<AuditResponse> response = shareClient.textAudit(auditRequest);
		log.info("shareClient textAudit response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}

	@Override
	public Boolean adjustPopularityRate(String goodsId, int targetValue) {
		ApiResult<Boolean> response = assetClient.adjustPopularityRate(goodsId, targetValue);
		log.info("assetClient adjustPopularityRate response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, Boolean.FALSE);
	}

	@Override
	public BusinessOrderResponse businessOrderInfoWithId(Long businessOrderId) {
		ApiResult<BusinessOrderResponse> response = payClient.businessOrderInfoWithId(businessOrderId);
		log.info("payClient businessOrderInfoWithId response:{}", JSONUtil.toJsonStr(response));
		return ApiResult.convert(response, null);
	}
}
